//
// Created by Tan Ke on 2023/11/18.
//

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>

struct item {
    int val1;
    int val2;
};

int comp(const void* v, const void* b)
{
    int v_val = *((int*)v);
    int b_val = *((int*)b);
    return v_val - b_val;
}

int maximumSum(int* nums, int numSize)
{
    qsort(nums, numSize, sizeof(int), comp);
    struct item map[90] = {0}; // 9 * 9 = 81

    for (int i = 0; i < 90; i ++) {
        map[i].val1 = -1;
        map[i] .val2 = -1;
    }

    /*
    for (int i = 0; i < numSize; i ++) {
        printf("%d ", nums[i]);
    }
    printf("\n");
    */

    for (int i = numSize - 1; i >= 0; i --) {
        int t = nums[i];
        int sum = 0;

        while (t > 0) {
            sum += t % 10;
            t /= 10;
        }

        if (map[sum].val1 == -1) {
            map[sum].val1 = nums[i];
        } else if (map[sum].val2 == -1) {
            map[sum].val2 = nums[i];
        }
    }

    int ret = -1;
    for (int i = 0; i < 90; i ++) {
        if (map[i].val1 > 0 && map[i].val2 > 0) {
            if (map[i].val1 + map[i].val2 > ret) {
                ret = map[i].val1 + map[i].val2;
            }
        }
    }

    return ret;
}

int main(void)
{
    int nums2[19] = {368,369,307,304,384,138,90,279,35,396,114,328,251,364,300,191,438,467,183};
    printf("%d\n", maximumSum(nums2, 19));
    return 0;
}